package com.sduept.nwld.mobile.rest;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sduept.core.ParamManager;
import com.sduept.core.dao.SuperDAO;
import com.sduept.nwld.mobile.manager.MobileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.inject.Inject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 故障统计手机端REST接口
 * @author Ningfw
 * @date 2017-09-27
 *
 */
@RestController
@RequestMapping("/faultStatistic/")
public class FaultStatisticServices {

	@Autowired
	private SuperDAO superDao;
	@Autowired
	private ParamManager paramCache;
	@Inject
	private MobileUtil mobileUtil;//SQL注入检测类
	
	/**
	 * 故障按月统计
	 * @return
	 */
	@SuppressWarnings("unchecked")

	@GetMapping("/countFaultByMonth/{year}")
	public String countFaultByMonth(@PathVariable("year") String year){
		try {
			year = mobileUtil.SQLInjectCheck(year);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
		String currentSql = "select count(*),date_format(c.faultTime, '%m'),c.faultPhase,c.faultTime from "
			+ "t_cim_fault c where date_format(c.faultTime, '%Y') = ? and c.status = 0 and voltage >= 500 group by "
			+ "date_format(c.faultTime, '%m'),c.faultPhase order by date_format(c.faultTime, '%m') asc";
		List<Object> currentYearList = (List<Object>) superDao.nativeQuery(currentSql, new Object[]{year});
		Map<String, Map<String,Integer>> monthMap = new HashMap<String, Map<String,Integer>>();
		int i = 0;//控制循环
		String mon = null;
		int onePhase = 0;
		int twoPhase = 0;
		int threePhase = 0;
		Map<String, Integer> numMap = new HashMap<String, Integer>();
		for(Object obj : currentYearList){//组织月、相别数据
			
			Object[] objs = (Object[]) obj;
			if(i == 0){
				mon = objs[1].toString();
			}
			i++;
			if(objs[1].toString().equals(mon)){//同月

				if((objs[2]==null)||(objs[2].toString().equals("其他"))||(objs[2].toString().length() == 1)){//空、其他算作单相
					onePhase += Integer.valueOf(objs[0].toString());
				}else if((objs[2].toString().length() == 2)&&!(objs[2].toString().equals("其他"))){
					twoPhase += Integer.valueOf(objs[0].toString());
				}else{
					threePhase += Integer.valueOf(objs[0].toString());
				}
			}else{//不同月
				
				numMap.put("onePhase", onePhase);
				numMap.put("twoPhase", twoPhase);
				numMap.put("threePhase", threePhase);
				monthMap.put(Integer.valueOf(mon)>=10?mon:mon.replace("0", ""), numMap);
				mon = objs[1].toString();
				onePhase = 0;
				twoPhase = 0;
				threePhase = 0;
				numMap = new HashMap<String, Integer>();
				if((objs[2]==null)||(objs[2].toString().equals("其他"))||(objs[2].toString().length() == 1)){//防止遗漏刚换月的第一条数据	空、其他算作单相
					onePhase += Integer.valueOf(objs[0].toString());
				}else if((objs[2].toString().length() == 2)&&!(objs[2].toString().equals("其他"))){
					twoPhase += Integer.valueOf(objs[0].toString());
				}else{
					threePhase += Integer.valueOf(objs[0].toString());
				}
			}
			if(i == currentYearList.size()){//最后一个

				numMap.put("onePhase", onePhase);
				numMap.put("twoPhase", twoPhase);
				numMap.put("threePhase", threePhase);
				monthMap.put(Integer.valueOf(mon)>=10?mon:mon.replace("0", ""), numMap);
			}
		}
		int[] arrData = new int[12];//每月的故障次数
		int[] oneData = new int[12];//每月的单相故障次数
		int[] twoData = new int[12];//每月的两相故障次数
		int[] threeData = new int[12];//每月的三相故障次数
		int faultTotal = 0;//故障总次数
		String[] xData = new String[12];
		String[] months = new String[]{"一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"};
		for(int j = 0; j < 12; j++){
			
			xData[j] = months[j];
			int monthTotal = 0;
			Map<String, Integer> phaseMap = monthMap.get(String.valueOf(j + 1));
			monthTotal = null == phaseMap?0:(phaseMap.get("onePhase") + phaseMap.get("twoPhase") + phaseMap.get("threePhase"));
			faultTotal += monthTotal;
			arrData[j] = monthTotal;
			oneData[j] = null == phaseMap?0:monthMap.get(String.valueOf(j + 1)).get("onePhase");
			twoData[j] = null == phaseMap?0:monthMap.get(String.valueOf(j + 1)).get("twoPhase");
			threeData[j] = null == phaseMap?0:monthMap.get(String.valueOf(j + 1)).get("threePhase");
		}
		JSONObject jso = new JSONObject();
		jso.put("total", faultTotal);
		jso.put("xAxis", xData);
		jso.put("data", arrData);
		jso.put("oneData", oneData);
		jso.put("twoData", twoData);
		jso.put("threeData", threeData);
		return jso.toString();
	}
	
	/**
	 * 故障按类型统计
	 */

	@GetMapping("/countFaultByType/{year}")
	public String countFaultByType(@PathVariable("year") String year){
		try {
			year = mobileUtil.SQLInjectCheck(year);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
		JSONArray jsa = new JSONArray();
		String countSql = "select c.faultType,count(*) from t_cim_fault c where date_format(c.faultTime,'%Y') = ? "
			+ "and c.status = 0 and voltage >= 500 group by c.faultType";
		List<?> countList = superDao.nativeQuery(countSql, new Object[]{year});
		for(Object obj : countList){
			
			String[] arr = new String[2];
			Object[] objs = (Object[]) obj;
			if(null == objs[0]){
				arr[0] = "未知";
			}else{
				arr[0] = objs[0].toString();
			}
			arr[1] = objs[1].toString();
			jsa.add(arr);
		}
		return jsa.toString();
	}
	
	/**
	 * 故障按故障原因统计
	 */

	@GetMapping("/countFaultByReason/{year}")
	public String countFaultByReason(@PathVariable("year") String year){
		try {
			year = mobileUtil.SQLInjectCheck(year);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
		JSONArray jsa = new JSONArray();
		String countSql = "select c.faultReson,count(*) from t_cim_fault c where date_format(c.faultTime,'%Y') = ? "
			+ "and c.status = 0 and voltage >= 500 group by c.faultReson";
		List<?> countList = superDao.nativeQuery(countSql, new Object[]{year});
		for(Object obj : countList){
			
			String[] arr = new String[2];
			Object[] objs = (Object[]) obj;
			if(null == objs[0]){
				arr[0] = "未知";
			}else{
				arr[0] = objs[0].toString();
			}
			arr[1] = objs[1].toString();
			jsa.add(arr);
		}
		return jsa.toString();
	}
}
